#include <my_header.h>
// 执行多条语句查询时，只执行一条语句查询，后续的语句不再执行
/* Usage:  ./4_sql_select*/
int main(void){                                  
    MYSQL mysql;
    MYSQL_ROW row;

    mysql_init(&mysql); // 初始化连接
    printf("%s", mysql_error(&mysql));

    mysql_set_character_set(&mysql, "utf8mb4");

    // 建立连接
    mysql_real_connect(&mysql, "localhost", "root", "123456", "homework", 0, NULL, 0);
    printf("%s", mysql_error(&mysql));

    // 查询cipi表的数据
    mysql_query(&mysql, "select * from capi");
    // mysql_query(&mysql, "select * from capi where id = 2");
    //mysql_query(&mysql, "select * from capi where age =18");
    //mysql_query(&mysql, "select * from capi where name = '张三' and age = 18 order by age");
    // mysql_query(&mysql, "select * from capi where gender = 'female'");
    // mysql_query(&mysql, "select name,group_concat(name) from capi where age >13 group by name");
    MYSQL_RES *result;
    result = mysql_store_result(&mysql);
    // 检查result是否为NULL
    if(result == NULL)
    {
        fprintf(stderr ,"mysql_store_result() 返回NULL %s \n", mysql_error(&mysql));
    }
    // 验证行数  
    my_ulonglong num_rows = mysql_num_rows(result);  
    if (num_rows == 0) {  
        printf("未找到任何行。\n");  
    } else {  
        printf("行数: %lu\n", num_rows);  
    }  
    // 获取列数
    unsigned int fields = mysql_num_fields(result);

    while((row = mysql_fetch_row(result)) != NULL)
    {
        for(int i=0; i<fields; i++)
        {
            printf("%s \t", row[i]);
        }
        printf("\n");
    }
    // 释放结果
    mysql_free_result(result);
    // 关闭连接
    mysql_close(&mysql);
    return 0;
}


